package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.LoginActivity;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.bean.CodeBean;
import com.zhongjian.technologycal.mall.http.BaseSubscriber;
import com.zhongjian.technologycal.mall.http.ExceptionHandle;
import com.zhongjian.technologycal.mall.http.MyApiService;
import com.zhongjian.technologycal.mall.http.RetrofitClient;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.Countdown;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/25.
 * 更换手机号
 */

public class NewTelPhoneActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.new_change_et_user_phone)
    EditText new_change_et_user_phone;
    @BindView(R.id.new_change_tv_send_code)
    TextView new_change_tv_send_code;
    @BindView(R.id.new_change_et_code)
    EditText new_change_et_code;
    private String payPasswordCertificate;


    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_change_new_telphone;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("新手机号");
        payPasswordCertificate = getIntent().getStringExtra("payPasswordCertificate");

    }

    @Override
    public void initPresenter() {


    }


    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    @OnClick(R.id.new_change_tv_send_code)
    public void onClickChangeSendCode() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(new_change_et_user_phone.getText().toString().trim())) {
            ToastUitl.showShort("手机号不能为空～");
            return;
        }
        new Countdown(new_change_tv_send_code, 60, "秒后重试", false);
        new_change_tv_send_code.setClickable(false);
        MyApiService service = RetrofitClient.getInstance(NewTelPhoneActivity.this).create(MyApiService.class);
        Map<String, String> maps = new HashMap<String, String>();
        maps.put("phoneNum", new_change_et_user_phone.getText().toString().trim());
        RequestBody body = RequestBody.create(
                okhttp3.MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(maps));

        // execute and add observable to RxJava
        RetrofitClient.getInstance(NewTelPhoneActivity.this).createBaseApi().execute(
                service.jsonSendCode(body), new BaseSubscriber<CodeBean>(NewTelPhoneActivity.this) {
                    @Override
                    public void onStart() {
                        super.onStart();
                        LoadingDialog.showDialogForLoading(NewTelPhoneActivity.this);
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        Log.e("Lyk", e.getMessage());

                    }

                    @Override
                    public void onNext(CodeBean codeBean) {
                        if (codeBean.getError_code() == 0) {
                            ToastUitl.showShort(codeBean.getError_message());

                        } else {
                            new Countdown(new_change_tv_send_code, 0, "秒后重试", false);
                            new_change_tv_send_code.setClickable(true);
                            ToastUitl.showShort(codeBean.getError_message());
                        }

                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        LoadingDialog.cancelDialogForLoading();
                    }
                });

    }

    /**
     *
     */
    @OnClick(R.id.new_change_tv_commit)
    public void onClickChangeCommit() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(new_change_et_code.getText().toString().trim())) {
            ToastUitl.showShort("验证码不能为空～");
            return;
        }

        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestUpdateUserNamePhone(
                        payPasswordCertificate,
                        UserUtils.getUserToken(),
                        new_change_et_code.getText().toString().trim(),
                        new_change_et_user_phone.getText().toString().trim(), new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                            }

                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        UserUtils.clear(getBaseContext());
                                        Intent intent = new Intent();
                                        intent.setClass(getBaseContext(), LoginActivity.class);
                                        startActivity(intent);
                                        finish();
                                    } else if (baseBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }


                            }
                        });


    }


    @Override
    protected void onResume() {
        super.onResume();
    }
}
